package org.hackystat.projecttrajectory.resource;

import static 
  org.hackystat.projecttrajectory.server.Authenticator.AUTHENTICATOR_SENSORBASECLIENTS_KEY;
import static org.hackystat.projecttrajectory.server.Authenticator.AUTHENTICATOR_DPDCLIENTS_KEY;
import java.util.Map;
import org.hackystat.dailyprojectdata.client.DailyProjectDataClient;
import org.hackystat.projecttrajectory.resource.estimate.EstimateManager;
import org.hackystat.projecttrajectory.server.Server;
import org.hackystat.sensorbase.client.SensorBaseClient;
import org.restlet.Context;
import org.restlet.data.CharacterSet;
import org.restlet.data.Language;
import org.restlet.data.MediaType;
import org.restlet.data.Request;
import org.restlet.data.Response;
import org.restlet.resource.Representation;
import org.restlet.resource.Resource;
import org.restlet.resource.StringRepresentation;
import org.restlet.resource.Variant;

/**
 * An abstract superclass for all ProjectTrajectory resources that supplies
 * common initialization processing.
 * 
 * @author Cam Moore
 * 
 */
public abstract class ProjectTrajectoryResource extends Resource {

  /** To be retrieved from the URL as the 'email' template parameter, or null. */
  protected String uriUser = null;

  /** To be retrieved from the URL as the 'project' template parameter, or null. */
  protected String project = null;

  /**
   * To be retrieved from the URL as the 'timestamp' template parameter, or
   * null.
   */
  protected String timestamp = null;

  /** The authenticated user, retrieved from the ChallengeResponse, or null */
  protected String authUser = null;
  
  /** The EstimateManager */
  protected EstimateManager estimateManager = null;

  /** The server. */
  protected Server server;

  /** The standard error message returned from invalid authentication. */
  protected String badAuth = "User is not admin and authenticated user does not not match URI user";

  /**
   * Provides the following representational variants: TEXT_XML.
   * 
   * @param context The context.
   * @param request The request object.
   * @param response The response object.
   */
  public ProjectTrajectoryResource(Context context, Request request, Response response) {
    super(context, request, response);
    if (request.getChallengeResponse() != null) {
      this.authUser = request.getChallengeResponse().getIdentifier();
    }
    this.server = (Server) getContext().getAttributes().get("ProjectTrajectoryServer");
    this.estimateManager = (EstimateManager) getContext().getAttributes().get("EstimateManager");
    this.uriUser = (String) request.getAttributes().get("user");
    this.project = (String) request.getAttributes().get("project");
    this.timestamp = (String) request.getAttributes().get("timestamp");
    getVariants().clear(); // copied from BookmarksResource.java, not sure why
                            // needed.
    getVariants().add(new Variant(MediaType.TEXT_XML));
  }

  /**
   * The Restlet getRepresentation method which must be overridden by all
   * concrete Resources.
   * 
   * @param variant The variant requested.
   * @return The Representation.
   */
  @Override
  public abstract Representation getRepresentation(Variant variant);

  /**
   * Creates and returns a new Restlet StringRepresentation built from xmlData.
   * The xmlData will be prefixed with a processing instruction indicating UTF-8
   * and version 1.0.
   * 
   * @param xmlData The xml data as a string.
   * @return A StringRepresentation of that xmldata.
   */
  public static StringRepresentation getStringRepresentation(String xmlData) { //NOPMD
    StringBuilder builder = new StringBuilder(500);
//  builder.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
    builder.append(xmlData);
    return new StringRepresentation(builder, MediaType.TEXT_XML, Language.ALL, CharacterSet.UTF_8);
  }

  /**
   * Returns a SensorBaseClient instance associated with the User in this
   * request.
   * 
   * @return The SensorBaseClient instance.
   */
  @SuppressWarnings("unchecked")
  public SensorBaseClient getSensorBaseClient() { //NOPMD
    Map<String, SensorBaseClient> userClientMap = (Map<String, SensorBaseClient>) this.server
        .getContext().getAttributes().get(AUTHENTICATOR_SENSORBASECLIENTS_KEY);
    return userClientMap.get(this.authUser);
  }

  /**
   * Returns a DailyProjectDataClient instance associated with the User in this
   * request.
   * 
   * @return The DailyProjectDataClient instance.
   */
  @SuppressWarnings("unchecked")
  public DailyProjectDataClient getDailyProjectDataClient() { //NOPMD
    Map<String, DailyProjectDataClient> userClientMap = (Map<String, DailyProjectDataClient>) 
    this.server.getContext().getAttributes().get(AUTHENTICATOR_DPDCLIENTS_KEY);
    return userClientMap.get(this.authUser);
  }

}
